package com.yunhe.sms.dao.system.impl;

import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.JdbcUtils;
import com.yunhe.sms.dao.system.RoleDao;
import com.yunhe.sms.entity.system.Role;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23 0023.
 */
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{

    @Override
    public Long save(Role role) {
        String sql = " insert into sys_role(name,status,remark,created_time,deleted) values(?,?,?,now(),0)";
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        Long id = null;
        try {
             id = qr.insert(sql, new ScalarHandler<Long>(), role.getName(), role.getStatus(), role.getRemark());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return id;

    }

    @Override
    public void delete(Long id) {
        String sql = " update sys_role set deleted = 1 where id = ? ";
        super.cud(sql,id);
    }


    @Override
    public void update(Role role) {
        String sql = " update sys_role set name=? ,status=? ,remark = ? where id = ? ";
        super.cud(sql,role.getName(),role.getStatus(),role.getRemark(),role.getId());
    }

    @Override
    public Role find(Long id) {
        String sql = " select id,name,status,remark from sys_role where deleted = 0 and id = ? ";
        Role role = super.queryOne(sql, id);
        return role;
    }

    @Override
    public List<Role> findAll() {
        String sql = " select id,name,status,remark from sys_role where deleted = 0 ";
        List<Role> roles = super.queryList(sql);
        return roles;
    }

    @Override
    public List<Role> findAllUsable() {
        String sql = " select id,name,status,remark from sys_role where deleted = 0 and status = 1 ";
        List<Role> roles = super.queryList(sql);
        return roles;
    }

    @Override
    public Pageable<Role> findPage(Integer offset, Integer size) {
        String listSql = " select id,name,status,remark from sys_role where deleted = 0 ";
        String countSql = " select count(*) from sys_role where deleted = 0 ";

        Pageable<Role> rolePageable = super.queryPage(listSql, countSql, offset, size);

        return rolePageable;
    }

    public void saveRolePermission(Long roleId,Long permissionId){
        String sql = " insert into sys_role_permission(role_id,permission_id) values(?,?) ";
        super.cud(sql,roleId,permissionId);
    }


    public void saveRolePermissionPL(Object[][] params){
        String sql = " insert into sys_role_permission(role_id,permission_id) values(?,?) ";
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        try {
            qr.batch(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public void deleteByRole(Long roleId){
        String sql = " delete from sys_role_permission where role_id = ? ";
        super.cud(sql,roleId);
    }
}
